import requests
from bs4 import BeautifulSoup

html_doc = """
<html><head><title id="one">The Dormouse's story</title></head>
<body>
<p class="title"><b>The Dormouse's story</b></p>

<p class="story">Once upon a time there were three little sisters; and their names were
<a href="http://example.com/elsie" class="sister" id="link1">Elsie</a>,
<a href="http://example.com/lacie" class="sister" id="link2">Lacie</a> and
<a href="http://example.com/tillie" class="sister" id="link3">Tillie</a>;
and they lived at the bottom of a well.</p>

<p class="story">...</p>
"""

#1.转类型 默认使用lxml的解析库
soup = BeautifulSoup(html_doc,'html.parser')

#2.通用解析方法
#find:返回符合查询条件的第一个标签
# result = soup.find('a')
# result = soup.find('p')
# result = soup.find(attrs={'class':'sister'})
# print(result)


#findall:所有的  返回列表
# result = soup.find_all('a')
# result = soup.find_all('a',limit=1)  #limit:限制
# result = soup.find_all(attrs={'class':'sister'})
# print(result)

#select_one:css的选择器  选第一个
# result = soup.select_one('.sister')
# print(result)

#select-CSS选择器---list
# result = soup.select('.sister')
# result = soup.select('#one')
# result = soup.select('head title')  #后代选择器
# result = soup.select('title,.title')  #组选择器
# result = soup.select('a[id="link3"]')  #属性选择器
# print(result)


#标签包裹的内容  取文本---list
# result = soup.select('b')[0].get_text()
# result = soup.select('b')[0].string
# print(result)  #The Dormouse's story

#去标签的属性
result = soup.select('#link1')[0].get('href')
print(result)  #http://example.com/elsie